package com.leadnews.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leadnews.dtos.Filedto;
import com.leadnews.dtos.Publishdto;
import com.leadnews.mapper.WemediaMapper;
import com.leadnews.pojo.MaterialFree;
import com.leadnews.pojo.User;
import com.leadnews.pojo.Wemedia;
import com.leadnews.pojo.WemediaMaterial;
import com.leadnews.service.MaterialFreeService;
import com.leadnews.service.WemediaMaterialService;
import com.leadnews.service.WemediaService;
import com.leadnews.utils.TokenFilter;
import com.leadnews.utils.oss.OssUtil;
import com.leadnews.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@CrossOrigin(origins = "*",maxAge = 3600)
@Slf4j
public class WemediaController {
    @Autowired
    OssUtil ossUtil;
    @Autowired
    WemediaMaterialService wemediaMaterialService;
    @Autowired
    WemediaService wemediaService;
    @Autowired
    TokenFilter tokenFilter;
    @Autowired
    WemediaMapper wemediaMapper;
    @Autowired
    MaterialFreeService materialFreeService;
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file,HttpServletRequest request){
        User token = tokenFilter.getToken(request);
        ResponseResult result = new ResponseResult();
        if (token==null){
            result.setCode(300);
            result.setErrorMessage("no access ,please login");
            return result;
        }
            //                    上传素材
            System.out.println(file.getOriginalFilename());
            String contentType = file.getContentType();
            String s = contentType.split("/")[1];
            System.out.println(file.getContentType());
            try {
                InputStream inputStream = file.getInputStream();
                String url = ossUtil.upload(inputStream, s);
                MaterialFree materialFree = new MaterialFree();
                materialFree.setName(file.getOriginalFilename());
                materialFree.setUid(token.getId()+"");
                materialFree.setUrl(url);
                materialFree.setSizeimage(file.getSize()+"");
                boolean save = materialFreeService.save(materialFree);
                if (save){
                    log.info("{}","图片保存成功");

                }
            }catch (Exception e){
                e.printStackTrace();
            }


        return null;
    }
    @PostMapping("/wemedia/removepic")
    public ResponseResult removepic(@RequestBody Filedto file, HttpServletRequest request){
        return wemediaMaterialService.removepic(file,request);
    }
    @PostMapping("/wemedia/publish")
    public ResponseResult publish(@RequestBody Publishdto publishdto,HttpServletRequest request){
        ResponseResult result = new ResponseResult();
        User token = tokenFilter.getToken(request);
        if (token==null){
            result.setCode(300);
            result.setErrorMessage("no access ,please login");
            return result;
        }
        if(StringUtils.isBlank(publishdto.getContent())){
            result.setCode(10);
            result.setErrorMessage("内容不能为空");
            return result;
        }

             result = wemediaService.publish(publishdto,token.getId()+"");

        return result;
    }
    @PostMapping("/findAllWemedia")
    public ResponseResult findAllWemedia(@RequestBody Map map){

    return wemediaService.findAllWemedia(map);
    }
}
